import Vue from 'vue'
import Router from 'vue-router'
import routes from './routers'
import store from '@/store'
import iView from 'view-design'
import Cookies from 'js-cookie'
import {
  setToken,
  getToken,
  canTurnTo
} from '@/libs/util'
import config from '@/config'
import {
  initRouter
} from '@/libs/router-util'
const {
  homeName
} = config

Vue.use(Router)
const router = new Router({
  routes,
  mode: 'hash' //history
})
const LOGIN_PAGE_NAME = 'login'

const turnTo = (to, access, next) => {
  initRouter()
  if (canTurnTo(to.name, access, routes)) next() // 有权限，可访问
  else {
    next({
      replace: true,
      name: 'error_401'
    })
  } // 无权限，重定向到401页面
}

router.beforeEach((to, from, next) => {
  iView.LoadingBar.start()
  if (to.name.indexOf('internal-') !== -1) {
    next()
  } else {
    const token = getToken();
    if (!token && to.name !== LOGIN_PAGE_NAME) {
      // 未登录且要跳转的页面不是登录页
      next({
        name: LOGIN_PAGE_NAME // 跳转到登录页
      })
    } else if (!token && to.name === LOGIN_PAGE_NAME) {
      // 未登陆且要跳转的页面是登录页
      next() // 跳转
    } else if (token && to.name === LOGIN_PAGE_NAME) {
      // 已登录且要跳转的页面是登录页
      next({
        name: homeName // 跳转到homeName页
      })
    } else {
      if (store.state.user.hasGetInfo) {
        turnTo(to, store.state.user.access, next)
      } else {
        store.dispatch('getUserInfo').then(user => {
          turnTo(to, user.access, next)
        }).catch(() => {
          next({
            name: 'login'
          })
        })
      }
    }
  }
})

router.afterEach(to => {
  iView.LoadingBar.finish()
  window.scrollTo(0, 0)
})

//push
const VueRouterPush = Router.prototype.push
Router.prototype.push = function push (to) {
    return VueRouterPush.call(this, to).catch(err => err)
}

//replace
const VueRouterReplace = Router.prototype.replace
Router.prototype.replace = function replace (to) {
  return VueRouterReplace.call(this, to).catch(err => err)
}

export default router